package com.myth;

public class ThrowTest {
    public static void main(String[] args) {
        try {
            Student s = new Student();
            s.regist(-1000);
            System.out.println(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Student{
    private int id;

    public void regist(int id) throws Exception {// 体现的是异常处理
        if (id > 0) {
            this.id = id;
        }else{
            // 声明一个异常对象
//            System.out.println("您输入的数据非法！");
            // 手动抛出异常对象
//            throw new RuntimeException("您输入的数据非法！");
//            throw new Exception("您输入的数据非法！");
            throw new MyException("不能输入负数！");
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                '}';
    }
}